package org.sith.taskrunner.gwt.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;
import org.sith.taskrunner.gwt.shared.domains.WrapperData;

import java.util.*;


public class TaskWrapperSelectorWidget extends Composite implements LeafValueEditor<List<WrapperData>> {


    interface TaskWrapperSelectorWidgetUiBinder extends UiBinder<HTMLPanel, TaskWrapperSelectorWidget> {
    }


    private final Map<String, WrapperData> wrappers = new HashMap<String, WrapperData>();

    private static TaskWrapperSelectorWidgetUiBinder ourUiBinder = GWT.create(TaskWrapperSelectorWidgetUiBinder.class);
    @UiField
    ListBox available;
    @UiField
    Button addButton;
    @UiField
    Button removeButton;
    @UiField
    ListBox checked;
    @UiField
    Button upItem;
    @UiField
    Button downItem;
    @UiField
    ListBox versionListBox;
    @UiField
    Label wrapperNameLabel;

    public TaskWrapperSelectorWidget() {
        initWidget(ourUiBinder.createAndBindUi(this));
    }


    @Override
    public void setValue(List<WrapperData> value) {
        for (WrapperData wrapperData : value) {
            String wrapperName = wrapperData.getWrapperName();
            checked.addItem(wrapperName);
            wrappers.put(wrapperName, wrapperData);
        }
    }

    @Override
    public List<WrapperData> getValue() {
        List<WrapperData> result = new ArrayList<WrapperData>();
        for (int i = 0; i < checked.getItemCount(); i++) {
            String itemText = checked.getItemText(i);
            result.add(wrappers.get(itemText));

        }
        return result;
    }

    @UiHandler("addButton")
    public void handleAddButtonClick(ClickEvent event) {
        moveEntry(available, checked);
    }

    @UiHandler("removeButton")
    public void handleRemoveButtonClick(ClickEvent event) {
        moveEntry(checked, available);
        if(checked.getItemCount()==0){
            versionListBox.setVisible(false);
            wrapperNameLabel.setVisible(false);
        }
    }

    @UiHandler("versionListBox")
    public void handleVersionListBoxValueChange(ChangeEvent event) {
        String checkedListBoxValue = getCheckBoxSelectedValue(checked);
        if (checkedListBoxValue == null) {
            return;
        }
        String versionValue = getCheckBoxSelectedValue(versionListBox);
        wrappers.get(checkedListBoxValue).setVersion(versionValue);
    }

    @UiHandler("checked")
    public void handleCheckedListBoxValueChange(ChangeEvent event) {
        String value = getCheckBoxSelectedValue(checked);
        wrapperNameLabel.setText(wrappers.get(value).getWrapperDescription());
        wrapperNameLabel.setVisible(true);
        versionListBox.setVisible(true);
    }

    @UiHandler("available")
    public void handleAvailableListBoxFocus(FocusEvent event) {
        checked.setSelectedIndex(-1);
    }

    @UiHandler("checked")
    public void handleCheckedListBoxFocus(FocusEvent event) {
        available.setSelectedIndex(-1);
    }


    private String getCheckBoxSelectedValue(ListBox checkBox) {
        int selectedIndex = checkBox.getSelectedIndex();
        return checkBox.getItemText(selectedIndex);
    }

    private void moveEntry(ListBox from, ListBox to) {
        int selectedIndex = from.getSelectedIndex();
        if (selectedIndex < 0) {
            return;
        }
        String itemText = from.getItemText(selectedIndex);
        from.removeItem(selectedIndex);
        to.addItem(itemText);
    }
}